package com.example.dell1.zyftpt_123jwq.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ActionMenuItemView;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import com.example.dell1.zyftpt_123jwq.R;
import com.example.dell1.zyftpt_123jwq.iface.CollectListener;
import com.example.dell1.zyftpt_123jwq.iface.FocusListener;
import com.example.dell1.zyftpt_123jwq.model.CollectModel;
import com.example.dell1.zyftpt_123jwq.model.FocusModel;

public class ViewArticleActivity extends AppCompatActivity {
    private WebView webView;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";
    private int resid;//资源id
    private int userid;//资源用户id
    Context context;
    private Boolean flagcollect = false;//收藏标志
    private Boolean flagfocus = false;//关注标志
    private CollectModel collectmodel;//收藏model
    private FocusModel focusmodel;//收藏model
    private SharedPreferences sp;//简单存储
    private String sessionID = "";  //sessionid


    FocusListener focusListener = new FocusListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            ActionMenuItemView item = (ActionMenuItemView) findViewById(R.id.menufocus);
            //根据mode中response返回的字符串区分返回结果
            switch (msg) {
                case "2":
                    System.out.println("----关注成功");
                    flagfocus = true;
                    item.setTitle("取消关注");
                    break;
                case "1":
                    System.out.println("----关注失败");
                    break;
                case "5":
                    System.out.println("----取消关注成功");
                    flagfocus = false;
                    item.setTitle("关注");
                    break;
                case "4":
                    System.out.println("----取消关注失败");
                    break;
                case "7":
                    System.out.println("----已关注");
                    flagfocus = true;
                    item.setTitle("关注收藏");
                    break;
                case "8":
                    System.out.println("----未关注");
                    flagfocus = false;
                    item.setTitle("关注");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    CollectListener listener = new CollectListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            ActionMenuItemView item = (ActionMenuItemView) findViewById(R.id.menucollect);
            //根据mode中response返回的字符串区分返回结果
            switch (msg) {
                case "2":
                    System.out.println("----收藏成功");
                    flagcollect = true;
                    item.setTitle("取消收藏");
                    break;
                case "1":
                    System.out.println("----收藏失败");
                    break;
                case "5":
                    System.out.println("----取消收藏成功");
                    flagcollect = false;
                    item.setTitle("收藏");
                    break;
                case "4":
                    System.out.println("----取消收藏失败");
                    break;
                case "7":
                    System.out.println("----已收藏");
                    flagcollect = true;
                    item.setTitle("取消收藏");
                    break;
                case "8":
                    System.out.println("----未收藏");
                    flagcollect = false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_article);
        context = ViewArticleActivity.this;

        resid = getIntent().getIntExtra("resid", 1);//获取传递的资源id
        userid = getIntent().getIntExtra("userid", 7);//获取传递的资源用户id
//        idolid = getIntent().getIntExtra("idolid", 7);//获取被关注用户的id

        webView = (WebView) findViewById(R.id.webView_article);
        webView.loadUrl(BASEURL + "article.php/show/index/id/" + resid);
//        webView.loadUrl(BASEURL + "article.php/show/index/id/" + userid);

        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        readSP();//读取sessionid
    }
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_view_article);
//        System.out.println("----查看文章详情");
//
//        resid  = getIntent().getIntExtra("resid",1);
//
//        webView = (WebView)findViewById(R.id.webView_article);
//        webView.loadUrl(BASEURL+"article.php/show/index/id/"+resid);
//
//    }

    private void readSP() {
        sessionID = sp.getString("sessionID", null);
        System.out.println("sessionID::::::::::::::" + sessionID);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);//加载菜单布局
        collectmodel = new CollectModel();//实例化对象
        collectmodel.exist("article", resid, sessionID, listener);//判断是否收藏
        focusmodel = new FocusModel();
        focusmodel.exist("userfocus", userid, sessionID, focusListener);//判断是否关注
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menucollect:
                //Toast.makeText(this, "文章收藏", Toast.LENGTH_SHORT).show();
                if (flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("article", resid, sessionID, listener);
                } else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("article", resid, sessionID, listener);
                }
                break;
            case R.id.menufocus:
                //Toast.makeText(this, "文章关注", Toast.LENGTH_SHORT).show();
                if (flagfocus)//如果已关注，则调用取消关注
                {
                    System.out.println("----准备关注");
                    focusmodel.unfocus("userfocus", userid, sessionID, focusListener);
                } else//如果未关注，则调用关注
                {
                    System.out.println("----准备取消关注");
                    focusmodel.focus("userfocus", userid, sessionID, focusListener);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
